﻿Imports Microsoft.VisualBasic
Imports System.Net
Imports System.Net.Mail
Imports System.Web

Public Class CConfig : Inherits Framework.CConfigBase

    'Encrypted
    Private Shared ReadOnly Property GmailUser As String
        Get
            Return ConfigOrEx("GmailUser")
        End Get
    End Property
    Private Shared ReadOnly Property GmailPassword As String
        Get
            Return ConfigOrEx("GmailPassword")
        End Get
    End Property
    Public Shared ReadOnly Property Gmail As SmtpClient
        Get
            Dim temp As New SmtpClient("smtp.gmail.com", 587) 'or 465  'Fresh instance: much slower, but safer
            temp.EnableSsl = True
            temp.UseDefaultCredentials = False
            temp.Credentials = New NetworkCredential(CConfig.GmailUser, CConfig.GmailPassword)
            Return temp
        End Get
    End Property


    Public Shared ReadOnly Property DropboxPath As String
        Get
            Return ConfigOrEx("DropboxPath")
        End Get
    End Property
    Public Shared ReadOnly Property DropboxPublic As String
        Get
            Return String.Concat(DropboxPath, "\public\")
        End Get
    End Property
    Public Shared ReadOnly Property DropboxArchive As String
        Get
            Return String.Concat(DropboxPath, "\archive\")
        End Get
    End Property
    Public Shared ReadOnly Property DropboxLiveData As String
        Get
            Return String.Concat(DropboxPath, "\backups\live_data\")
        End Get
    End Property


    Public Shared Function ItunesUrl(name As String) As String
        Return "http://mikeconnellministries.com/" & DropboxUrl(name).Substring(DropboxUrl.Length)
    End Function
    Public Shared Function DropboxUrl(name As String) As String
        Dim sb As New Text.StringBuilder(CConfig.DropboxUrl)
        name = name.Replace("\", "/")
        If name.StartsWith("/") Then name = name.Substring(1)
        If name.Contains("/") Then
            Dim ss As New List(Of String)(name.Split("/"))
            For Each s As String In ss
                sb.Append(UrlEncode(s).Replace("+", "%20"))
                If ss.Count <> 1 + ss.IndexOf(s) Then sb.Append("/")
            Next
            Return sb.ToString
        Else
            name = UrlEncode(name)
            Return String.Concat(CConfig.DropboxUrl, name)
        End If
    End Function
    Public Shared Function DropboxUrl() As String
        Return Config("DropboxUrl", "https://dl.dropboxusercontent.com/u/53152016/")
    End Function
    Public Shared Function UrlEncode(s As String) As String
        Return System.Web.HttpUtility.UrlEncode(s)
    End Function

    'Public Shared Function ContentDelivery(name As String, streaming As Boolean) As String
    '    If streaming Then
    '        Return String.Concat("http://s3-ap-southeast-1.amazonaws.com/streaming.mikeconnellministries.com/", name) 'String.Concat("http://streaming.mikeconnellministries.com/", name)
    '    Else
    '        Return String.Concat("http://downloads.mikeconnellministries.com/", name)
    '    End If
    'End Function


    Public Shared Function ProductUrl(p As CProduct) As String
        Dim s As String = p.ProductType.Plural.Replace(" ", "_").ToLower
        If p.ProductTypeId_ = EProductType.ConferenceSet Then s = "sets"
        Return String.Concat(s, "/", p.ProductId, "/", SafeFriendlyName(p.ProductName), ".aspx")
    End Function
    Public Shared Function TranscriptUrl(p As CProduct) As String
        Return String.Concat("transcript/", p.ProductId, "/", SafeFriendlyName(p.ProductName), ".aspx")
    End Function
    Public Shared Function SafeFriendlyName(s As String) As String
        If String.IsNullOrEmpty(s) Then Return String.Empty
        Return System.Web.HttpUtility.UrlEncode(s.Replace("#", String.Empty).Replace(" ", "-").Replace("&", "and").Replace(":", String.Empty).Replace("'", String.Empty).Replace("""", String.Empty).Replace("(", String.Empty).Replace(")", String.Empty).Replace(",", String.Empty).Replace("\", "-").Replace("/", "-").Replace("--", "-").Replace("--", "-"))
    End Function

    Public Shared Function Mp3(p As CProduct) As String
        Return String.Concat("http://mikeconnellministries.com/", SafeFriendlyName(p.ProductName), "/", p.ProductId, "/mp3.aspx")
    End Function

    Public Shared Function WebsiteUrl() As String
        Return Config("WebsiteUrl", "http://mikeconnellministries.com")
    End Function
    Public Shared Function WebsiteUrl_Product(p As CProduct, Optional mobile As Boolean = False) As String
        Return String.Concat(WebsiteUrl, IIf(mobile, "/mobile/", "/"), ProductUrl(p))
    End Function
    Public Shared Function WebsiteUrl_ProductImage(p As CProduct) As String
        Return String.Concat(WebsiteUrl, "/uploads/products/", p.ProductImageOrParent)
    End Function
    Public Shared Function WebsiteUrl_ProductThumb(p As CProduct) As String
        Return String.Concat(WebsiteUrl, "/uploads/products/", p.ProductThumbOrParent)
    End Function
    Public Shared Function WebsiteUrl_ProductTiny(p As CProduct) As String
        Return String.Concat(WebsiteUrl, "/uploads/products/", p.ProductTinyOrParent)
    End Function
    Public Shared Function WebsiteUrl_Signature() As String
        Return String.Concat(WebsiteUrl, "/images/sarah/signature.jpg")
    End Function
    Public Shared Function WebsiteUrl_Header() As String
        Return String.Concat(WebsiteUrl, "/images/sarah/email_header.jpg")
    End Function
    Public Shared Function WebsiteUrl_Footer() As String
        Return String.Concat(WebsiteUrl, "/images/sarah/email_footer.jpg")
    End Function
    Public Shared Function WebsiteUrl_AudioIcon() As String
        Return String.Concat(WebsiteUrl, "/images/sarah/audio_icon.jpg")
    End Function
    Public Shared Function WebsiteUrl_ManualIcon() As String
        Return String.Concat(WebsiteUrl, "/images/sarah/manual_icon.jpg")
    End Function
    Public Shared Function WebsiteUrl_VideoIcon() As String
        Return String.Concat(WebsiteUrl, "/images/sarah/video_icon.jpg")
    End Function
    Public Shared Function WebsiteUrl_TextIcon() As String
        Return String.Concat(WebsiteUrl, "/images/text.png")
    End Function
    Public Shared Function WebsiteUrl_TranscriptPaged(product As CProduct) As String
        Return String.Concat(WebsiteUrl, "/", TranscriptUrl(product))
    End Function
    Public Shared Function WebsiteUrl_TranscriptAsHtm(productId As Integer) As String
        Return String.Concat(WebsiteUrl, "/transcript.aspx?productId=", productId)
    End Function


    Public Shared Function WebsitePath() As String
        If Not IsNothing(System.Web.HttpContext.Current) Then Return System.Web.HttpContext.Current.Server.MapPath("~/")
        Return ConfigOrEx("WebsitePath")
    End Function
    Public Shared Function WebsitePath_Signature() As String
        Return String.Concat(WebsitePath, "/images/sarah/signature.jpg")
    End Function
    Public Shared Function WebsitePath_Header() As String
        Return String.Concat(WebsitePath, "/images/sarah/email_header.jpg")
    End Function
    Public Shared Function WebsitePath_Footer() As String
        Return String.Concat(WebsitePath, "/images/sarah/email_footer.jpg")
    End Function
    Public Shared Function WebsitePath_AudioIcon() As String
        Return String.Concat(WebsitePath, "/images/sarah/audio_icon.jpg")
    End Function
    Public Shared Function WebsitePath_ManualIcon() As String
        Return String.Concat(WebsitePath, "/images/sarah/manual_icon.jpg")
    End Function
    Public Shared Function WebsitePath_VideoIcon() As String
        Return String.Concat(WebsitePath, "/images/sarah/video_icon.jpg")
    End Function
    Public Shared Function WebsitePath_TextIcon() As String
        Return String.Concat(WebsitePath, "/images/text.png")
    End Function
    Public Shared Function WebsitePath_ProductThumb(p As CProduct) As String
        Return String.Concat(WebsitePath, "/uploads/products/", p.ProductThumbOrParent)
    End Function


#Region "Paypal"
    Public Shared Function Sandbox() As Boolean
        Return ConfigBool("Sandbox", True)
    End Function
    Public Shared Function Paypal() As String
        Return String.Concat("https://www.", IIf(CConfig.Sandbox, "sandbox.", String.Empty), "paypal.com/cgi-bin/webscr")
    End Function
    Public Shared Function BuyNow(ByVal o As COrder) As String
        If o.Product.ProductSpeakerId = ESpeaker.ShaneWillard Then
            Return "http://" & o.Product.Speaker.SpeakerWebsite
        Else
            Return String.Concat(Paypal, "?cmd=_s-xclick&hosted_button_id=", o.Product.ProductPaypalCode, "&custom=", IIf(Integer.MinValue = o.OrderId, "orderId", o.OrderId), IIf(CConfig.Sandbox, "&ipn_test=1", String.Empty), "&invoice=", Encode(o.CustomerEmail))
        End If
    End Function
    Public Shared Function Donate(ByVal o As COrder) As String
        If o.Product.ProductSpeakerId = ESpeaker.ShaneWillard Then
            Return External_ShaneWillardDonate()
        Else
            Return String.Concat(Paypal, "?cmd=_s-xclick&hosted_button_id=", DonateButtonCode, "&custom=", IIf(Integer.MinValue = o.OrderId, "orderId", o.OrderId), IIf(CConfig.Sandbox, "&ipn_test=1", String.Empty), "&invoice=", Encode(o.CustomerEmail))
        End If
    End Function
    Public Shared Function DonateButtonCode() As String
        Return "BQ4KNKH7RSNKC" '"Q2GLTTBANY4XQ"
    End Function
    Public Shared Function DonateButtonJeremy() As String
        Return DonateButtonCode()
        'Return "LTAJTECS57D5Y"
    End Function
    Private Shared Function Encode(ByVal s As String) As String
        If String.IsNullOrEmpty(s) Then Return String.Empty
        Return System.Web.HttpUtility.UrlEncode(s)
    End Function
#End Region

#Region "Other External"
    'External
    'Ping: https://phobos.apple.com/WebObjects/MZFinance.woa/wa/pingPodcast?id=523085771
    Public Shared Function External_Podcast() As String
        Return "http://itunes.apple.com/nz/podcast/mike-connell-ministries/id523085771"
    End Function
    Public Shared Function External_Facebook() As String
        Return "http://facebook.com/MikeConnellMinistries"
    End Function
    Public Shared Function External_FacebookLike(p As CProduct) As String
        Return String.Concat("http://www.facebook.com/plugins/like.php?href=", HttpUtility.UrlEncode(CConfig.WebsiteUrl_Product(p)))
    End Function
    Public Shared Function External_FacebookShare(p As CProduct) As String
        'Return String.Concat("http://www.facebook.com/sharer.php?u=", HttpUtility.UrlEncode(CConfig.WebsiteUrl_Product(p)), "&t=", HttpUtility.UrlEncode(p.ProductName))
        Return String.Concat("http://www.facebook.com/sharer.php?u=", HttpUtility.UrlEncode(CConfig.WebsiteUrl_Product(p)))
    End Function
    Public Shared Function External_FacebookShare_Transcript(p As CProduct) As String
        Return String.Concat("http://www.facebook.com/sharer.php?u=", HttpUtility.UrlEncode(CConfig.WebsiteUrl_TranscriptAsHtm(p.ProductId)))
    End Function


    Public Shared Function External_Twitter() As String
        Return "http://twitter.com/#!/PsMikeConnell"
    End Function
    Public Shared Function External_Donations() As String
        Return External_Paypal(DonateButtonCode)
    End Function
    Public Shared Function External_DonationsJeremy() As String
        Return External_Paypal(DonateButtonJeremy)
    End Function
    Public Shared Function External_ShaneWillardDonate() As String
        Return "https://www.shanewillardpartners.org/partners/index.cfm?do=contribute"
    End Function
    Public Shared Function External_Paypal(buttonCode As String) As String
        Return String.Concat("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=", buttonCode)
    End Function
    Public Shared Function External_Review(p As CProduct) As String
        Return External_Review(p.ProductSmashwordsId)
    End Function
    Public Shared Function External_Review(id As Integer) As String
        Return String.Concat("http://www.smashwords.com/books/writereview/", id, "?ref=MikeConnellMinistries")
    End Function
    Public Shared Function External_ReviewAmazon(p As CProduct) As String
        Return External_ReviewAmazon(p.ProductAmazonId)
    End Function
    Public Shared Function External_ReviewAmazon(id As String) As String
        Return String.Concat(External_Amazon(id), "#customerreviews")
    End Function

    Public Shared Function External_Smashwords(id As Integer) As String
        Return String.Concat("http://www.smashwords.com/books/view/", id, "?ref=MikeConnellMinistries")
    End Function
    Public Shared Function External_Smashwords() As String
        Return "http://www.smashwords.com/profile/view/MikeConnellMinistries?ref=MikeConnellMinistries"
    End Function
    Public Shared Function External_Amazon(id As String) As String
        'Return String.Concat("http://www.amazon.com/", HttpContext.Current.Server.UrlEncode(name.Replace(" ", "-").Replace("'", "").Replace(":", "")), "/dp/", id)
        Return String.Concat("http://www.amazon.com/gp/product/", id, "/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=", id, "&linkCode=as2&tag=mikeconnmini-20")
    End Function
    Public Shared Function External_Amazon(id As String, name As String) As String
        Return External_Amazon(id) 'name not used anymore
    End Function
    Public Shared Function External_Amazon() As String
        'Return "http://www.amazon.com/Mike-Connell/e/B008EGXHO8/"  '"http://www.amazon.com/s/field-keywords=mike+connell+ministries"
        Return "http://www.amazon.com/gp/search/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&camp=1789&creative=9325&index=aps&keywords=B008EGXHO8&linkCode=as2&tag=mikeconnmini-20"
    End Function
    Public Shared Function External_Vimeo() As String
        Return "http://vimeo.com/mikeconnell/videos"
    End Function
    Public Shared Function External_YouTube() As String
        Return "http://www.youtube.com/channel/UCczUj6I5fZM2MCCHgfY-lwg"
    End Function
    Public Shared Function External_CreateSpace(id As Integer) As String
        Return "https://www.createspace.com/" & id
    End Function
    Public Shared Function External_CreateSpace(p As CProduct) As String
        Return External_CreateSpace(p.ProductCSBookIdOrParent)
    End Function
    Public Shared Function External_CreateSpace2(p As CProduct) As String
        Return External_CreateSpace(p.ProductCSBookId2OrParent)
    End Function
    Public Shared Function External_CreateSpace3(p As CProduct) As String
        Return External_CreateSpace(p.ProductCSBookId3OrParent)
    End Function
#End Region

End Class
